package com.example.applets.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.applets.entity.Order;
import com.example.applets.entity.Product;
import com.example.applets.service.impl.OrderServiceImpl;
import com.example.applets.service.impl.ProductServiceImpl;
import com.example.applets.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 云烟成雨丶
 * @since 2020-10-14
 */
@Controller
@RequestMapping("/applets/order")
public class OrderController {
    @Autowired
    private OrderServiceImpl orderService;

    @Autowired
    private ProductServiceImpl productService;

    @RequestMapping("/page")
    @ResponseBody
    //查询分页
    public AjaxResult selectPage(@RequestParam(value = "status", required = false) String status, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        Page<Order> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        if (!status.equals("")) {
            wrapper.eq("status", Integer.parseInt(status));
        }
        List<Order> list = orderService.page(page, wrapper).getRecords();
        int count = (orderService.count(wrapper) - 1) / pageSize + 1;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", list);
        map.put("total", count);
        if (list.size() == 0)
            return AjaxResult.error("暂无数据");
        return AjaxResult.success(map);
    }

    @RequestMapping("/delete")
    @ResponseBody
    //逻辑删除
    public AjaxResult deleteProduct(@RequestParam("id") long id) {
        Order order = orderService.getById(id);
        order.setStatus(0);
        if (!orderService.updateById(order))
            return AjaxResult.error("关闭失败!");
        return AjaxResult.success("关闭成功!");
    }

    @RequestMapping("/add")
    @ResponseBody
    //逻辑删除
    public AjaxResult addOrder(Order order) {
        System.out.println("该订单的的信息为" + order);
        //获得该商品
        Product product = productService.getById(order.getProdid());
        //获得库存量,小于购买量,返回失败
        if (product.getStockcount() < order.getNum())
            return AjaxResult.error("购买失败,库存不够");
        //修改库存
        product.setStockcount(product.getStockcount() - order.getNum());
        productService.updateById(product);//保存修改
        //随机生成单号
        Random random = new Random();
        String num = "";
        for (int a = 1; a <= 12; a++) {
            num = num + random.nextInt(10);
        }
        //设置单号
        order.setLogisticsno(num);
        if (!orderService.save(order))
            return AjaxResult.success("购买失败!");
        return AjaxResult.success("购买成功!");
    }
}

